/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.web.controller;

import cc.altius.utils.ExcelUtils.BodyRow;
import cc.altius.utils.ExcelUtils.CellInfo;
import cc.altius.utils.ExcelUtils.Parameter;
import cc.altius.utils.ExcelUtils.WorkSheet;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.NumberFormat;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.view.document.AbstractJExcelView;

/**
 *
 * @author mudassar
 */
@Controller
public class ExportReportExelController extends AbstractJExcelView {

    @Override
     protected void buildExcelDocument(Map model, WritableWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("inside buildExcelDocument");
        Colour headerBackground = Colour.LAVENDER;
        int titleRow = 0;
        int titleColumn = 1;

        WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 16, WritableFont.BOLD);
        WritableFont boldFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
        WritableFont bodyFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD);

        WritableCellFormat headerCell = new WritableCellFormat(boldFont);
        headerCell.setBackground(headerBackground);
        headerCell.setWrap(true);
        headerCell.setBorder(Border.ALL, BorderLineStyle.THIN);

        int row, col;

        Map<String, Object> map = (Map<String, Object>) model.get("map");

        List<WorkSheet> workSheets = (List<WorkSheet>) map.get("WorkBook");

        for (WorkSheet thisSheet : workSheets) {
            WritableSheet sheet = workbook.createSheet(thisSheet.getTitle(), 0);
            row = titleRow;
            col = titleColumn;
            WritableCellFormat titleCell = new WritableCellFormat(titleFont);

            // write Report title
            sheet.addCell(new Label(col, row, thisSheet.getTitle(), titleCell));
            sheet.mergeCells(col, row, col + 3, row);
            sheet.setRowView(row, 500);
            for (int x = 1; x < thisSheet.getColumns(); x++) {
                sheet.addCell(new Label(x + titleColumn, row, "", titleCell));
            }

            NumberFormat integerFormat = new NumberFormat("0");
            NumberFormat decimalFormat = new NumberFormat("0.##");
            NumberFormat percFormat = new NumberFormat("0.## %");
            WritableCellFormat bodyCell = null;


            // write the parameters if any
            if (thisSheet.getParameters() != null) {
                row += 2;

                col = titleColumn;
                for (Parameter tmpParameter : thisSheet.getParameters()) {
                    bodyCell = new WritableCellFormat(bodyFont);
                    bodyCell.setWrap(true);
                    bodyCell.setAlignment(tmpParameter.getLabel().getAlign());
                    sheet.addCell(new Label(col, row, tmpParameter.getLabel().getValue().toString(), bodyCell));

                    bodyCell = new WritableCellFormat(bodyFont);
                    bodyCell.setWrap(true);
                    bodyCell.setAlignment(tmpParameter.getValue().getAlign());
                    sheet.addCell(new Label(col + 1, row, tmpParameter.getValue().getValue().toString(), bodyCell));
                    sheet.mergeCells(col + 1, row, col + 2, row);
                    row++;

                }
                for (int x = 2; x < thisSheet.getColumns(); x++) {
                    sheet.addCell(new Label(x + titleColumn, row, "", titleCell));
                }

            }
            // now write the report body
            row += 2;
            col = titleColumn;
            jxl.CellView cellView;
            int i = 0;
            for (BodyRow thisRow : thisSheet.getBodyRows()) {
                col = titleColumn;
                for (CellInfo info : thisRow.getValues()) {
                    if (info.getValue() != null) {
                        if (info.getType() == CellInfo.INTEGER) {
                            bodyCell = new WritableCellFormat(bodyFont, integerFormat);
                            bodyCell.setWrap(true);
                            bodyCell.setBorder(Border.ALL, BorderLineStyle.THIN);
                            bodyCell.setBackground(thisRow.getRowColour());
                            bodyCell.setAlignment(info.getAlign());
                            sheet.addCell(new Number(col, row, ((java.lang.Number) info.getValue()).intValue(), bodyCell));
                        } else if (info.getType() == CellInfo.DOUBLE) {
                            bodyCell = new WritableCellFormat(bodyFont, decimalFormat);
                            bodyCell.setWrap(true);
                            bodyCell.setBorder(Border.ALL, BorderLineStyle.THIN);
                            bodyCell.setBackground(thisRow.getRowColour());
                            bodyCell.setAlignment(info.getAlign());
                            sheet.addCell(new Number(col, row, ((java.lang.Number) info.getValue()).doubleValue(), bodyCell));
                        } else if (info.getType() == CellInfo.PERCENTAGE) {
                            bodyCell = new WritableCellFormat(bodyFont, percFormat);
                            bodyCell.setWrap(true);
                            bodyCell.setBorder(Border.ALL, BorderLineStyle.THIN);
                            bodyCell.setBackground(thisRow.getRowColour());
                            bodyCell.setAlignment(info.getAlign());
                            sheet.addCell(new Number(col, row, ((java.lang.Number) info.getValue()).doubleValue(), bodyCell));
                        } else {
                            bodyCell = new WritableCellFormat(bodyFont);
                            bodyCell.setWrap(true);
                            bodyCell.setBorder(Border.ALL, BorderLineStyle.THIN);
                            bodyCell.setBackground(thisRow.getRowColour());
                            bodyCell.setAlignment(info.getAlign());
                            sheet.addCell(new Label(col, row, info.getValue().toString(), bodyCell));
                        }
                    }

                    if (i == thisSheet.getBodyRows().size() - 1) {
                        cellView = sheet.getColumnView(col);
                        // cellView.setAutosize(true);
                        cellView.setSize(info.getSize());
                        sheet.setColumnView(col, cellView);
                    }

                    col++;
                }
                row++;
                i++;

            }
        }
        // set the reponse header
        String fileName = (String) map.get("FileName");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".xls\"");
    }
    
}
